package com.cat.dataStructure02;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimum-time-to-repair-cars/
 * @create 2025/10/13 20:09
 * @since JDK17
 */

public class Solution13 {
    boolean check(int[] ranks, int target, long time) {
        int sum = 0, n = ranks.length;
        for (int num : ranks) {
            sum += Math.sqrt(time / num);
        }

        return sum >= target;
    }
    public long repairCars(int[] ranks, int cars) {
        Arrays.sort(ranks);
        long l = 0, r = (long) ranks[0] * cars * cars, mid;
        while (l + 1 < r) {
            mid = l + r >> 1;
            if (check(ranks, cars, mid)) {  // 能完成
                r = mid;
            } else {
                l = mid;
            }
        }

        return r;
    }
}
